package com.example.myapp;

import android.content.Context;

import java.io.*;

/**
 * Created by 艳双 on 2015/12/18.
 * project:TakePhoto
 * user:lisa[FR]
 * Date:2015/12/18
 */
public class UserUtil {
    /**
     * 保存用户信息
     *
     * @param context
     * @param user
     */
    public static void saveUser(Context context, User user) {
        try {
            FileOutputStream fos = new FileOutputStream(context.getFilesDir()
                    + "/user.txt");
            ObjectOutputStream os = new ObjectOutputStream(fos);

            os.writeObject(user);
            os.flush();
            os.close();
        } catch (Exception e) {
        }
    }

    /**
     * 获取用户信息
     *
     * @param context
     * @return
     */
    public static User getUser(Context context) {
        User user = new User();
        try {
            FileInputStream fos = new FileInputStream(context.getFilesDir()
                    + "/user.txt");
            ObjectInputStream os = new ObjectInputStream(fos);
            user = (User) os.readObject();
            os.close();

        } catch (Exception e) {
        }
        return user;
    }

    /**
     * 获取userid
     */
    public static String getUserId(Context ctx) {
        // String userId = getUser(ctx).getUser_id();
        String userId = getUser(ctx).getUser_id();
        if (userId == null)
            return "";
        return userId;
    }

    /**
     * 移除用户的信息
     *
     * @param context
     * @return
     */
    public static void removeUser(Context context) {
        File file = new File(context.getFilesDir() + "/user.txt");
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 退出
     * @param context
     */
    public static void quit(Context context) {
        removeUser(context);

    }

//    public static void putchannelId(Context context, String channelId) {
//        SharedPreferences preferences = context.getSharedPreferences(
//                "PushYunReceiver", Context.MODE_PRIVATE);
//        SharedPreferences.Editor edit = preferences.edit();
//
//        edit.putString("channelId", channelId);
//        edit.commit();
//
//    }

//    public static String getchannelId(Context context) {
//        SharedPreferences preferences = context.getSharedPreferences(
//                "PushYunReceiver", Context.MODE_PRIVATE);
//
//        return preferences.getString("channelId", "");
//
//    }
public static void setCompanyInfologo(Context context, String avatar) {

    CompanyInfoModel c = getCompanyInfo(context);
    c.setLogo(avatar);

    saveCompanyInfoModel(context, c);

}
    /**
     * 从本地文件获取user对象
     */
    public static CompanyInfoModel getCompanyInfo(Context context) {
        CompanyInfoModel user = new CompanyInfoModel();
        try {
            FileInputStream fos = new FileInputStream(context.getFilesDir() + "/CompanyInfo.txt");
            ObjectInputStream os = new ObjectInputStream(fos);
            user = (CompanyInfoModel) os.readObject();
            os.close();

        } catch (Exception e) {
        }
        return user;
    }
    /**
     * 保存企业信息到本地文件
     */
    public static void saveCompanyInfoModel(Context context, CompanyInfoModel info) {
        try {
            FileOutputStream fos = new FileOutputStream(context.getFilesDir() + "/CompanyInfo.txt");
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(info);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void setuseravatar(Context context, String avatar) {
        User u = getUser(context);
        u.setAvatar(avatar);
        saveUser(context, u);

    }

}
